package com.huawei.petSystemdemo.ui.mOrder;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huawei.hms.hwid.I;
import com.huawei.petSystemdemo.GlobalViewModel;
import com.huawei.petSystemdemo.R;
import com.huawei.petSystemdemo.ShoppingAdressActivity;
import com.huawei.petSystemdemo.adapter.OrderPetAdapter;
import com.huawei.petSystemdemo.adapter.ShoppingAdapter;
import com.huawei.petSystemdemo.bean.Adress;
import com.huawei.petSystemdemo.bean.Pets;
import com.huawei.petSystemdemo.bean.Shopping;
import com.huawei.petSystemdemo.bean.User;
import com.huawei.petSystemdemo.data.AdressRepository;
import com.huawei.petSystemdemo.data.CartRepository;
import com.huawei.petSystemdemo.data.ShoppingRepository;
import com.huawei.petSystemdemo.data.UserRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class OrderSureActivity extends AppCompatActivity {

    private GlobalViewModel global = GlobalViewModel.getGlobal();

    private RecyclerView order_sure_recycle;
    private Button tijiao;
    private LinearLayout order_address;
    private TextView add_name;
    private TextView add_tel;
    private TextView order_adds;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_sure);

        Intent intent = getIntent();
        int addId = intent.getIntExtra("addId", -1);

        order_sure_recycle = findViewById(R.id.order_sure_recycle);
        tijiao = findViewById(R.id.jieshuang);
        order_address = findViewById(R.id.order_address);
        add_name = findViewById(R.id.add_name);
        add_tel = findViewById(R.id.add_tel);
        order_adds = findViewById(R.id.order_adds);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(RecyclerView.VERTICAL);
        order_sure_recycle.setLayoutManager(manager);

        List<Pets> petsList = global.seleted.getValue();

        OrderPetAdapter adapter = new OrderPetAdapter(petsList, this);
        order_sure_recycle.setAdapter(adapter);

        AdressRepository.getById(global.user.getAddId(), new Consumer<Adress>() {
            @Override
            public void accept(Adress adress) {

            }
        });

        //设置收货地址
        if(addId != -1) {
            AdressRepository.getById(addId, new Consumer<Adress>() {
                @Override
                public void accept(Adress adress) {
                    add_name.setText(adress.getUserName());
                    add_tel.setText(adress.getTel());
                    order_adds.setText(adress.getProvince() + " " + adress.getAdressContent());
                }
            });
        }


        order_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(OrderSureActivity.this, ShoppingAdressActivity.class);
                intent.putExtra("order", 1);
                startActivity(intent);
            }
        });

        tijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //用户资金与订单总价
                if(global.user.getIntegral() < global.getTotalPrice()) {
                    Toast.makeText(OrderSureActivity.this, "您积分不够，请充值后再购买", Toast.LENGTH_SHORT).show();
                } else {

                    //1、扣除用户积分
                    UserRepository.updateIntegral(global.user.getUsername(), global.getTotalPrice(), new Consumer<String>() {
                        @Override
                        public void accept(String s) {

                            //数据处理
                            if(s.equals("ok")) {
                                //获取购物车中的商品Id
                                List<Integer> listPetId = new ArrayList<>();
                                List<Pets> petsList = global.seleted.getValue();
                                for (int i = 0; i < petsList.size(); i++) {
                                    listPetId.add(petsList.get(i).getId());
                                }

                                int order_add;
                                if(addId != -1) {
                                    order_add = addId;
                                } else {
                                    order_add = global.user.getAddId();
                                }

                                //2、保存订单
                                CartRepository.saveOrder(global.user.getId(), global.getTotalPrice(), listPetId, 1,order_add, new Consumer<String>() {
                                    @Override
                                    public void accept(String data) {

                                        //数据处理
                                        if(data.length() != 0) {
                                            //获取购物车中的项目Id
                                            List<Integer> listShoppingId = new ArrayList<>();
                                            for (int i = 0; i < global.shoppingSelected.getValue().size(); i++) {
                                                listShoppingId.add(global.shoppingSelected.getValue().get(i).getId());
                                            }

                                            //3、删除购物车中的数据
                                            CartRepository.deleteShopping(listShoppingId, new Consumer<String>() {
                                                @Override
                                                public void accept(String data) {

                                                    //清空global中的数据
                                                    List<Shopping> global_shopping_clear = new ArrayList<>();
                                                    global.shoppingSelected.setValue(global_shopping_clear);
                                                    List<Pets> global_pets_clear = new ArrayList<>();
                                                    global.seleted.setValue(global_pets_clear);
                                                }
                                            });
                                        }
                                    }
                                });
                            }
                        }
                    });
                    //完成订单，更新用户信息
                    UserRepository.getUser(global.user.getId(), new Consumer<User>() {
                        @Override
                        public void accept(User user) {
                            Toast.makeText(OrderSureActivity.this, user.getIntegral() + "", Toast.LENGTH_SHORT).show();
                            global.setUser(user);
                        }
                    });
                }
            }
        });
    }
}